package com.bdqn.dao;

import java.sql.*;

public class LoginDao {
    public static void main(String[] args){
        Connection con=null;
       // Statement stmt=null;
        ResultSet rs=null;
        PreparedStatement pstmt=null;
        //第一步加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接对象
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school?serverTimezone=GMT-8", "root", "123");
            //写SQL语句
            String sql = "SELECT COUNT(*) FROM student WHERE studentname=?AND phone=?";
            //执行SQL
            //stmt = con.createStatement();
            pstmt = con.prepareStatement(sql);
            pstmt.setObject(1,"王柳");
            pstmt.setObject(2,"5555");
            rs = pstmt.executeQuery();
            //处理结果集  System.out.println()快捷方法sout+回车
            int count = 0;
            while (rs.next()) {
                count = rs.getInt(1);
                //释放资源
            }
            if (count > 0) {
                System.out.println("登录成功");

        }else {
                System.out.println("登录失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pstmt!=null){
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

